package com.wkbb.sports.controller;

import com.alibaba.excel.EasyExcel;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.GradeDto;
import com.wkbb.common.dto.basic.GradeTreeDto;
import com.wkbb.common.dto.basic.StudentInfoDto;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.handler.JsonUtil;
import com.wkbb.common.utils.StringUtil;
import com.wkbb.sports.dto.*;
import com.wkbb.sports.enums.SportsModelTypeEnum;
import com.wkbb.sports.enums.SportsProjectKindEnum;
import com.wkbb.sports.feign.BasicFeignService;
import com.wkbb.sports.mapper.BasicMapper;
import com.wkbb.sports.model.MetaverseActivityLocation;
import com.wkbb.sports.service.AppService;
import com.wkbb.sports.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(description = "公共基础数据接口")
@RequestMapping("/sports/{apiVersion}/common")
@Slf4j
public class CommonController extends BaseController {

    @Autowired
    private BasicFeignService basicFeignService;

    @Autowired
    private DictService dictService;
    @Autowired
    private AppService appService;
    @Autowired
    private BasicMapper basicMapper;



    @ApiOperation(value = "学生列表")
    @RequestMapping(value = "getStudentInfoList", method = RequestMethod.GET)
    public ResultDTO<List<StudentInfoDto>> getStudentInfoList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校id", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "班级id", required = false) @RequestParam(name = "classId", required = false) Long classId) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("orgId",orgId);
            params.put("classId",classId);
            List<StudentInfoDto> slist = basicMapper.getStudentInfoList(params);
            return new ResultDTO<>(slist);
        } catch (Exception e) {
            log.error("***********CommonController.getExaminePageList******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }
    @ApiOperation(value = "年级列表")
    @RequestMapping(value = "getGradeList", method = RequestMethod.GET)
    public ResultDTO<List<GradeDto>> getGradeList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校id", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "年级类型(1幼儿园 2小学 3中职 4初中 5高中 6高职 7本科 8完中 9九年一贯 10十二年一贯)", required = true) @RequestParam(name = "gradeType", required = true) Integer gradeType) {
        log.info("CommonController orgId:{} gradeType:{}", orgId,gradeType);

        try {
            if(orgId==1)
            {
                gradeType = 10;
            }
            return basicFeignService.getGradeList(orgId,gradeType);
        } catch (Exception e) {
            log.error("***********CommonController.getExaminePageList******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "年级班级树")
    @RequestMapping(value = "getClassTree", method = RequestMethod.GET)
    public ResultDTO<List<GradeTreeDto>> getClassTree(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "学校id", required = true) @RequestParam(name = "orgId", required = true) Long orgId){
        try {
            return basicFeignService.getClassTree(orgId);
        }catch (Exception e){
            log.error("***********CommonController.getTarget******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "考察点列表")
    @RequestMapping(value = "/getTargetList", method = RequestMethod.GET)
    public ResultDTO<List<DictDto>> getTargetList(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion){
        try{
            Map<String,Object> params = new HashMap<>();
            params.put("dictCode","target");
            return dictService.getDict(params);
        }catch (Exception e){
            log.error("***********CommonController.getTargetList******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "单位列表")
    @RequestMapping(value = "/getUnit", method = RequestMethod.GET)
    public ResultDTO<List<DictDto>> getUnit(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion){
        try{
            Map<String,Object> params = new HashMap<>();
            params.put("dictCode","unit");
            return dictService.getDict(params);
        }catch (Exception e){
            log.error("***********CommonController.getUnit******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "体育项目类型列表")
    @RequestMapping(value = "/getSportsProjectList", method = RequestMethod.GET)
    public ResultDTO<List<SportsProjectDto>> getSportsProjectKindList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion)
    {
        List<SportsProjectDto> list = new ArrayList<SportsProjectDto>();
        for(SportsProjectKindEnum typeEnum : SportsProjectKindEnum.values()){
            SportsProjectDto sportsProjectDto = new SportsProjectDto();
            sportsProjectDto.setProjectId(typeEnum.getValue());
            sportsProjectDto.setProjectName(typeEnum.getDesc());

            list.add(sportsProjectDto);
        }

        return new ResultDTO<List<SportsProjectDto>>(list);
    }

    @ApiOperation(value = "体育项目模式列表")
    @RequestMapping(value = "/getSportsModelTypeList", method = RequestMethod.GET)
    public ResultDTO<List<SportsModelTypeDto>> getSportsModelTypeList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
			@ApiParam(value = "是否只获取项目") @RequestParam(name = "isProject", required = false) boolean isProject)
    {
        List<SportsModelTypeDto> list = new ArrayList<SportsModelTypeDto>();
        for(SportsModelTypeEnum typeEnum : SportsModelTypeEnum.values()){
        	if(typeEnum.getProjectKind() == 1 && typeEnum != SportsModelTypeEnum.MOCKS) {
        		continue;
        	}
        	if (!isProject || typeEnum.getIsProject()){
                SportsModelTypeDto sportsModelTypeDto = new SportsModelTypeDto();
                sportsModelTypeDto.setId(typeEnum.getValue());
                sportsModelTypeDto.setName(typeEnum.getDesc());
                sportsModelTypeDto.setProjectKind(typeEnum.getProjectKind());
                list.add(sportsModelTypeDto);
            }
        }

        return new ResultDTO<List<SportsModelTypeDto>>(list);
    }
    
    @ApiOperation(value = "获取APP最新版本号")
    @RequestMapping(value = "/getLastVersion", method = RequestMethod.GET)
    public ResultDTO<?> getLastVersion(
    		 @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
    		 @ApiParam(value = "操作系统(1安卓2iOS)", required = true) @RequestParam(name = "osKind", required = true) Byte osKind)throws Exception{
    	log.info("getLastVersion(osKind:" + osKind + ")");
    	AppVersionDTO appVersion = appService.getLastVersion(osKind);
    	return new ResultDTO<AppVersionDTO>(appVersion);
    }
    
    @ApiOperation(value = "添加APP最新版本号")
    @RequestMapping(value = "/addAppVersion", method = RequestMethod.GET)
    public ResultDTO<?> addAppVersion(
    		 @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
    		 @ApiParam(value = "操作系统(1安卓2iOS)", required = true) @RequestParam(name = "osKind", required = true) Byte osKind,
    		 @ApiParam(value = "版本名", required = true) @RequestParam(name = "version", required = true) String version,
    		 @ApiParam(value = "版本号", required = true) @RequestParam(name = "versionId", required = true) Integer versionId,
    		 @ApiParam(value = "版本描述", required = true) @RequestParam(name = "description", required = true) String description,
    		 @ApiParam(value = "下载路径", required = true) @RequestParam(name = "downloadPath", required = true) String downloadPath)throws Exception{
    	log.info("addAppVersion(osKind:" + osKind + ", version:" + version + "versionId:" + versionId + ", description: " + description + ", downloadPath: " + downloadPath + ")");
    	AppVersionDTO appVersion = new AppVersionDTO();
    	appVersion.setOsKind(osKind);
    	appVersion.setDownloadPath(downloadPath);
    	appVersion.setVersion(version);
    	appVersion.setVersionId(versionId);
    	appVersion.setDescription(description);
    	appService.addAppVersion(appVersion);
    	return new ResultDTO<AppVersionDTO>(appVersion);
    }

}